<?php
namespace app\admin\controller\contents;
use app\common\controller\Backend;

//Banner管理
class Banner extends Backend{
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Banner');
    }
    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
            foreach ($list as $k => &$v) {
                $v['fullurl'] = $cdnurl . $v['path'];
                $v['atime']=date("Y-m-d H:i:s",$v['atime']);
                $v['isshow']=$v['ishide']==0 ? '是' : '否';
            }
            unset($v);
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 选择附件
     */
    public function select()
    {
        if ($this->request->isAjax()) {
            return $this->index();
        }
        return $this->view->fetch();
    }



    /**
     * 删除附件
     * @param array $ids
     */
    public function del($ids = "")
    {
        if ($ids) {
            \think\Hook::add('upload_delete', function ($params) {
                $attachmentFile = ROOT_PATH . '/public' . $params['path'];
                if (is_file($attachmentFile)) {
                    @unlink($attachmentFile);
                }
            });
            $attachmentlist = $this->model->where('id', 'in', $ids)->select();
            foreach ($attachmentlist as $attachment) {
                \think\Hook::listen("upload_delete", $attachment);
                $attachment->delete();
            }
            $this->success();
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
    public function add($ids = NULL){
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            $params = array_filter(array_intersect_key(
                $params,
                array_flip(array('path','linkurl'))
            ));
            $params['porder']=(int)$_POST['row']['porder'];
            if($params['porder']>127){
                $params['porder']=127;
            }
            if($params['porder']<1){
                $params['porder']=0;
            }
            $params['ishide']=(int)post('ishide');
            $params['atime']=TIMESTAMP;
            if ($params) {
                Model('banner')->insert($params);
                $this->success();
            }
            $this->error();
        }
        return $this->view->fetch();
    }
    public function edit($ids = NULL){
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            $params = array_filter(array_intersect_key(
                $params,
                array_flip(array('path','linkurl'))
            ));
            $params['porder']=(int)$_POST['row']['porder'];
            if($params['porder']>127){
                $params['porder']=127;
            }
            if($params['porder']<1){
                $params['porder']=0;
            }
            $params['ishide']=(int)post('ishide');
            if ($params) {
                Model('banner')->where(array('id'=>$ids))->update($params);
                $this->success();
            }
            $this->error();
        }
        if($ids<1) {
            $this->error('广告不存在');
        }
        $row =Model('banner')->where(array('id'=>$ids))->find();
        $this->view->assign('row',$row);
        return $this->view->fetch();
    }
}
